package com.panda.business.service;

import com.panda.business.domain.CustomerInfo;
import com.panda.business.domain.req.CustomerVo;
import com.panda.business.mapper.CustomerMapper;
import com.panda.common.core.domain.entity.SysUser;
import com.panda.common.utils.SecurityUtils;
import com.panda.framework.domain.base.DBPage;
import com.panda.framework.domain.base.PageBean;
import com.panda.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional // 支持事务
public class CustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private ISysUserService accountMapper;

	public PageBean<CustomerInfo> queryCustomerInfo(CustomerVo info) {
		 
		DBPage page = new DBPage(info.getPageSize(), (info.getPageNo() - 1) * info.getPageSize());
		
		List<CustomerInfo> resultList = customerMapper.queryCustomerInfo(page,info.getSuppliername());
		Integer totalResult = customerMapper.queryCustomerInfoCount(page,info.getSuppliername());
		int pageTotal = totalResult % info.getPageSize() == 0 ? totalResult / info.getPageSize(): totalResult / info.getPageSize() + 1;

		PageBean<CustomerInfo> pageBean = new PageBean<CustomerInfo>(info.getPageSize(), info.getPageNo(), pageTotal, resultList);

		return pageBean;
	}

 
	public CustomerInfo getCustomerInfoByid(Integer id){
		CustomerInfo user = customerMapper.findCustomerInfoByid(1);
		return user;
	}
	
	public void insert(CustomerInfo user) {
		customerMapper.insert(user);
		if(null != user.getId()){
			SysUser sysUser = new SysUser();
			sysUser.setUserName(user.getAdmin());
			String encryptPassword = SecurityUtils.encryptPassword("123456");
			sysUser.setPassword(encryptPassword);
			sysUser.setNickName(user.getAdmin());
			sysUser.setUserType("00"); //系统用户
			sysUser.setCid(user.getId());
			sysUser.setStatus("0");
			accountMapper.insertUser(sysUser);
		}
	}

	public void update(CustomerInfo user) {
		customerMapper.update(user);
	}

	public void delete(Integer id) {
		CustomerInfo user = new CustomerInfo();
		user.setId(id);
		customerMapper.delete(user);
	}
	
	public CustomerInfo getCustomerInfoByDeviceId(String deviceId){
		return customerMapper.getCustomerInfoByDeviceId(deviceId);
	}
	
	
	
}
